//
//  ZMSearchCollectionViewFlowLayout.swift
//  LoveBeen
//
//  Created by pg on 2017/8/9.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMSearchCollectionViewFlowLayout: UICollectionViewFlowLayout {

    var keyWordsArray: [String]?{
        didSet{
            
        }
    }
    
    
    override init() {
        super.init()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:- 每次布局的准备,在collectionview第一次显示的时候首先调用,当cell的位置要发生变化的时候也会首先调用这个方法
    override func prepare() {
        
        
        super.prepare()
        
        
        
    }
    
    //MARK:- 方法返回no,那么每次item位置发生改变的时候都只会调用这个方法,不会再向下调用其他方法
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        
       
        return true
    }
   //MARK:- 返回rect范围内的布局属性,掉完collectionViewContentSize之后就会调用这个方法
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
      
       
        guard  let attrArr = super.layoutAttributesForElements(in: rect) else{
            return nil
        }
        
       
        var lastAttr : UICollectionViewLayoutAttributes?
        
        var lineNum: Int = 0
        for attr in attrArr {
            
            if attr.representedElementCategory != .cell{
                break
            }
            //获取当前布局对象的indexpath
            let indexpath = attr.indexPath
            
            //仅对第一组做大小布局
            if indexpath.section != 0{
                break
            }
            
            //获取当前indexpath对应的cell 要显示的字符串
            guard let textArray = self.keyWordsArray ,textArray.count != 0 else {
                break
            }
            let text = textArray[indexpath.row]
           
            //获取当前text对应的size
            let textSize = (text as NSString).size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18)])
            
            attr.size.width = textSize.width + 13
            attr.size.height = textSize.height + 10
            
            if lastAttr == nil{
                lastAttr = attr
                attr.frame.origin.x = CGFloat(20)
                attr.frame.origin.y = CGFloat(70)
                continue
            }
            attr.frame.origin.x = lastAttr!.frame.origin.x + lastAttr!.size.width + 10
            
            if attr.frame.maxX > UIScreen.main.bounds.width {
                attr.frame.origin.x = CGFloat(20)
                lineNum += 1
            }
            attr.frame.origin.y = CGFloat(lineNum) * (attr.size.height + 10) + CGFloat( 70)
            lastAttr = attr
       }
       return attrArr
    }
    //MARK:- 返回指定位置的item的布局属性
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attr = super.layoutAttributesForItem(at: indexPath)
        
        return attr
        
    }
    //MARK:- 每次cell将要显示的时候就会自动调用这个方法
    override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        let attr = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
        return attr
    }
    //MARK:- 每次cell将要消失的时候就会自动调用这个方法
    override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attr = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
        return attr
    }
    
    
   
}
